package com.yanyu.space.common.sleuth.util;

import com.yanyu.space.common.core.util.WebUtil;
import org.springframework.cloud.sleuth.Span;
import org.springframework.cloud.sleuth.TraceContext;
import org.springframework.cloud.sleuth.brave.bridge.BraveSpan;

import javax.servlet.ServletRequest;

/**
 * @author yanyu
 */
public class TracerWebUtil {

    public static TraceContext getTraceContextInRequest(ServletRequest request) {
        TraceContext traceContext = (TraceContext) request.getAttribute(TraceContext.class.getName());
        return traceContext;
    }

    public static TraceContext getTraceContextInRequest() {
        return getTraceContextInRequest(WebUtil.getRequest());
    }

    public static brave.Span getSpanInRequest(ServletRequest request) {
        BraveSpan braveSpan = (BraveSpan) request.getAttribute(Span.class.getName());
        return BraveSpan.toBrave(braveSpan);
    }

    public static brave.Span getSpanInRequest() {
        return getSpanInRequest(WebUtil.getRequest());
    }
}
